package Client;
import javax.swing.*;

import Entities.Admin;
import Entities.Voter;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class frontPage {
	
	public frontPage() {
		JFrame Mframe = new JFrame("Online Voting System!");
		Mframe.setSize(600,600);
		Mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Mframe.setResizable(true);
		Board mpanel = new Board(Mframe);
		Mframe.setContentPane(mpanel);		
		Mframe.setVisible(true);
	}
	
	public static void main(String[] args){
		JFrame Mframe = new JFrame("Online Voting System!");
		Mframe.setSize(600,600);
		Mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Mframe.setResizable(true);
		Board mpanel = new Board(Mframe);
		Mframe.setContentPane(mpanel);		
		Mframe.setVisible(true);
	}
}

@SuppressWarnings("serial")
class Board extends JPanel implements ActionListener{	
	JLabel welcome, name, pass, errname, errpass;
	JButton login, register, exit;
	JTextField userField;
	JPasswordField passField;
	String username,password;
	JFrame mframe;
	public Board(JFrame mframe){
		this.mframe = mframe;
		setLayout(null); 	
		setBackground(Color.WHITE);

		welcome = new JLabel("Online Voting System");
		login = new JButton("Login");
		register = new JButton("Register");
		exit = new JButton("Exit");
		name = new JLabel("Username:");
		pass = new JLabel("Password:");
		errname = new JLabel("Please enter your username!");
		errpass = new JLabel("Username or password combination incorrect!");
		userField = new JTextField();
		passField = new JPasswordField(10);

		welcome.setBounds(200,20,400,30);		// x/y/width/length
		login.setBounds(30, 250, 85, 30);
		register.setBounds(120, 250, 100, 30);
		exit.setBounds(270, 250, 100, 30);
		name.setBounds(40,100,70,30);		// x/y/width/length
		pass.setBounds(40,140,70,30);		
		errname.setBounds(300,300,200,30);		// x/y/width/length
		errpass.setBounds(200,320,300,30);		
		userField.setBounds(120, 100, 200, 30);
		passField.setBounds(120, 140, 200, 30);

		login.addActionListener(this);
		register.addActionListener(this);
		exit.addActionListener(this);

		welcome.setFont(new Font("Dialog", Font.BOLD, 24));
		errname.setForeground(Color.RED);  
		errpass.setForeground(Color.RED);

		add(welcome);   
		add(login);
		add(register);
		add(exit);
		add(name);  
		add(pass);
		add(errname);
		add(errpass); 
		add(userField);
		add(passField);

		exit.setVisible(true);
		userField.setVisible(true);
		passField.setVisible(true);
		name.setVisible(true);
		pass.setVisible(true);
		errname.setVisible(false);
		errpass.setVisible(false);
		register.setVisible(true);
		login.setVisible(true);
		welcome.setVisible(true);
	}

	protected void resetFocus() {
		passField.requestFocusInWindow();
	}

	public void actionPerformed(ActionEvent e){	
		if(e.getActionCommand().equals("Login")){	
			String inputuname = userField.getText();
			String inputpass = new String(passField.getPassword());
//			DBMethods dbm = new DBMethods();
//			Voter v = dbm.getStudent(inputuname, inputpass);
			VotingClient client = new VotingClient();
			String incoming = "";
			try {
				incoming = client.sendMessage("login#"+inputuname+"#"+inputpass);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Voter v = null;
			Admin a = null;
			if(!incoming.equals(""))
			{
				String incomingSp [] = incoming.split("#");
				if(incomingSp[0].equals("student"))
				{
					v = new Voter(incomingSp[1], incomingSp[2], incomingSp[3], incomingSp[4], Integer.parseInt(incomingSp[5]), incomingSp[6]);
				}
				else
				{
					a = new Admin(Integer.parseInt(incomingSp[1]), incomingSp[2], incomingSp[3]);
				}
			}
			if (v!=null) {
				if(v.getActivated() == 2)
				{
					mframe.getContentPane().removeAll();
					mframe.repaint();
					JPanel p = new Boardb(v,mframe);
					p.setBounds(0, 0, 600, 600);
					mframe.add(p);//.setV3();
					mframe.validate();
					mframe.repaint();
					System.out.println("Success Student! You typed the right password.");
				}
				else if(v.getActivated()==1)
				{
					mframe.getContentPane().removeAll();
					mframe.repaint();
					JPanel p = new confirmReg(v,mframe);
					p.setBounds(0, 0, 600, 600);
					mframe.add(p);//.setV3();
					mframe.validate();
					mframe.repaint();
				}
				else
				{
					errpass.setVisible(true);
					System.out.println("Invalid username or password. Try again.");
				}
				
			} else if(a!=null) {
				mframe.getContentPane().removeAll();
				mframe.repaint();
				JPanel p = new BoardAd(a,mframe);
				p.setBounds(0, 0, 600, 600);
				mframe.add(p);//.setV3();
				mframe.validate();
				mframe.repaint();
				System.out.println("Success Admin! You typed the right password.");
			}else {
				errpass.setVisible(true);
				System.out.println("Invalid username or password. Try again.");
			}
			
			passField.selectAll();
			resetFocus();
			
		}else if(e.getActionCommand().equals("Register"))
			{
				mframe.getContentPane().removeAll();
				mframe.repaint();
				JPanel p = new Boardc(mframe);
				p.setBounds(0, 0, 600, 600);
				mframe.add(p);//.setV3();
				mframe.validate();
				mframe.repaint();
			}
		else if(e.getActionCommand().equals("Exit"))
			System.exit(0);
		else {
			System.out.println("bye!!");
		}
	}
}
